package com.panda.rentHouse.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.panda.common.service.BaseServiceImpl;
import com.panda.common.util.Pager;
import com.panda.rentHouse.dao.MenuDao;
import com.panda.rentHouse.pojo.Menu;

@Service
@Transactional
public class MenuServiceImpl extends BaseServiceImpl implements MenuService {
	
	@Autowired
	private MenuDao menuDao;

	@Override
	public List<Map<String, Object>> getMenuList() throws Exception {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<Menu> parentList = menuDao.getParentMenuList();
		for(int i=0;i<parentList.size();i++) {
			Menu parentMenu = parentList.get(i);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("level", parentMenu.getLevel());
			map.put("name", parentMenu.getMenuName());
			map.put("code", parentMenu.getMenuCode());
			map.put("url", parentMenu.getUrl());
			List<Map<String, Object>> childList = menuDao.getChildMenuList(parentMenu.getMenuCode());
			map.put("childs", childList);
			result.add(map);
		}
		return result;
	}

	@Override
	public Pager getMenuPager(Map<String, Object> params, int pageNo, int pagesize) throws Exception {
		List<Object> args = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append(" select * \n");
		sql.append(" ,(case when status = 1 then '有效' when status = 0 then '无效' else '' end) statusStr \n");
		sql.append(" from menu where 1 = 1 \n");
		Integer status = (Integer)params.get("status");
		String searchValue = (String)params.get("searchValue");
		if(status!=-1) {
			sql.append(" and status = ? \n");
			args.add(status);
		}
		if(!"".equals(searchValue)) {
			sql.append(" and (locate(?, menuName)>0 or locate(?, menuCode)>0) \n");
			args.add(searchValue);
			args.add(searchValue);
		}
		sql.append(" order by seq ");
		return menuDao.getPagerBySql(sql.toString(), pageNo, pagesize, args.toArray());
	}

}
